﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SAPbouiCOM;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace SAPBoAddonUI
{
    /// <summary>
    /// 通过srf文件映射成的B1Form对应的语言包
    /// </summary>
    /// <author>Webb.Wen</author>
    /// <lastedit>2011-07-12 13:57:23</lastedit>
    public class B1FormLanguagePackage
    {
        /*
         * <form appformnumber="AVA_BudgetForm" FormType="AVA_BudgetForm" type="0" BorderStyle="0" uid="AVA_LBGT_" title="LenovoGIS - Budget" visible="1" default_button="1" pane="0" color="0" left="212" top="0" width="1058" height="622" client_width="1042" client_height="584" AutoManaged="0" SupportedModes="15" ObjectType="" mode="1">
         */

        string _appformnumber;
        [XmlAttribute("Appformnumber")]
        public string Appformnumber
        {
            get { return _appformnumber; }
            set { _appformnumber = value; }
        }
        string _FormType;
        [XmlAttribute("FormType")]
        public string FormType
        {
            get { return _FormType; }
            set { _FormType = value; }
        }
        string _type;
        [XmlAttribute("Type")]
        [KeyAttribute("type")]
        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }
        string _uid;
        [XmlAttribute("Uid")]
        public string Uid
        {
            get { return _uid; }
            set { _uid = value; }
        }
        string _title;
        [XmlAttribute("窗体标题")]
        [TranslateAttribute("title")]
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
        string _ObjectType;
        [XmlAttribute("ObjectType")]
        public string ObjectType
        {
            get { return _ObjectType; }
            set { _ObjectType = value; }
        }
        /// <summary>
        /// 初始化私有集合
        /// </summary>
        public B1FormLanguagePackage()
        {
            _items = new List<Item>();
        }

        List<Item> _items;

        public List<Item> Items
        {
            get { return _items; }
            set { _items = value; }
        }

        #region methods
        public override string ToString()
        {
            return new ObjectToXML<B1FormLanguagePackage>().FromObjectToXML(this);
        }
        /// <summary>
        /// 保存语言包到文件
        /// </summary>
        /// <param name="file">完整路径</param>
        public void SaveTo(string file)
        {
            XmlDocument xml = new XmlDocument();
            xml.LoadXml(this.ToString());
            xml.Save(file);
            return;
            using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                {
                    sw.Write(new ObjectToXML<B1FormLanguagePackage>().FromObjectToXML(this));
                    sw.Close();
                }
            }
        }
        /// <summary>
        /// 从文件加载语言包
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public B1FormLanguagePackage LoadFrom(string file)
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(file);
            return new ObjectToXML<B1FormLanguagePackage>().FromXMLToObject(xml.InnerXml);
            
            using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                {
                    string xmlstring = sr.ReadToEnd();
                    return new ObjectToXML<B1FormLanguagePackage>().FromXMLToObject(xmlstring);
                }
            }
        }
        #endregion
    }
    /// <summary>
    /// 对应item
    /// </summary>
    public class Item
    {
        /// <summary>
        /// 初始化私有集合
        /// </summary>
        public Item()
        {
            _columns = new List<Column>();
            _validValues = new List<ValidValue>();
        }
        #region fields
        List<Column> _columns;

        public List<Column> Columns
        {
            get { return _columns; }
            set { _columns = value; }
        }
        List<ValidValue> _validValues;

        public List<ValidValue> ValidValues
        {
            get { return _validValues; }
            set { _validValues = value; }
        }
        private string _uid;
        /// <summary>
        /// 对应uid
        /// </summary>
        [XmlAttribute("控件ID")]
        [KeyAttribute("uid")]
        public string Uid
        {
            get { return _uid; }
            set { _uid = value; }
        }
        private string _type;
        /// <summary>
        /// 对应type
        /// </summary>
        [XmlAttribute("Type")]
        public string Type
        {
            get { return _type; }
            set { _type = value; }
        }
        private string _caption;
        /// <summary>
        /// 对应caption，将这个字段扁平化处理
        /// </summary>
        [XmlAttribute("显示名称")]
        [TranslateAttribute("caption")]
        [SubNodeAttribute("specific")]
        public string Caption
        {
            get { return _caption; }
            set { _caption = value; }
        }
        private string _description;
        /// <summary>
        /// 对应description
        /// </summary>
        [XmlAttribute("描述")]
        [TranslateAttribute("description")]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        #endregion

        public override string ToString()
        {
            return this.Caption ?? "";
        }
    }
    /// <summary>
    /// 对应Matrix的column
    /// </summary>
    public class Column
    {
        /// <summary>
        /// 初始化私有集合
        /// </summary>
        public Column()
        {
            _validValues = new List<ValidValue>();
        }
        #region fields
        List<ValidValue> _validValues;

        public List<ValidValue> ValidValues
        {
            get { return _validValues; }
            set { _validValues = value; }
        }

        private string _uid;
        /// <summary>
        /// 对应uid
        /// </summary>
        [XmlAttribute("控件ID")]
        [KeyAttribute("uid")]
        public string Uid
        {
            get { return _uid; }
            set { _uid = value; }
        }
        private string _type;
        /// <summary>
        /// 对应type
        /// </summary>
        [XmlAttribute("Type")]
        public string Type
        {
            get
            {
                if (String.IsNullOrEmpty(_type))
                {
                    return "113";
                }
                return _type;
            }
            set { _type = value; }
        }
        private string _title;
        /// <summary>
        /// 对应title
        /// </summary>
        [XmlAttribute("显示名称")]
        [TranslateAttribute("title")]
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
        private string _description;
        /// <summary>
        /// 对应description
        /// </summary>
        [XmlAttribute("描述")]
        [TranslateAttribute("description")]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        #endregion

        public override string ToString()
        {
            return this.Title ?? "";
        }
    }
    /// <summary>
    /// 对应ComboBox的ValidValue，可以在Item里，也可以在Column里
    /// </summary>
    public class ValidValue
    {
        #region fields
        string _value;
        /// <summary>
        /// 对应value
        /// </summary>
        [XmlAttribute("Value")]
        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
        string _description;
        /// <summary>
        /// 对应description
        /// </summary>
        [XmlAttribute("描述")]
        [TranslateAttribute("description")]
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }
        #endregion

        public override string ToString()
        {
            return String.Format("{0} - {1}", this.Value, this.Description);
        }
    }
    /// <summary>
    /// 对应srf中的元素属性名称
    /// </summary>
    /// <author>Webb.Wen</author>
    /// <lastedit>2011-07-13 14:02:46</lastedit>
    [AttributeUsage(AttributeTargets.Property)]
    public class TranslateAttribute : Attribute
    {
        //通过Type的GetField获取FieldInfo,然后通过GetCustomAttributes获取相关的attribute. 

        public TranslateAttribute(string element)
        {
            this._element = element;
        }
        private string _element;

        public string Element
        {
            get { return _element; }
            set { _element = value; }
        }
    }
    /// <summary>
    /// 对应srf中的元素属性名称
    /// 是否负责主键角色（uid或者type）
    /// 原则上只多只能有一个属性有此特性
    /// </summary>
    /// <author>Webb.Wen</author>
    /// <lastedit>2011-07-13 14:48:56</lastedit>
    [AttributeUsage(AttributeTargets.Property)]
    public class KeyAttribute : Attribute
    {
        //通过Type的GetField获取FieldInfo,然后通过GetCustomAttributes获取相关的attribute. 

        public KeyAttribute(string element)
        {
            this._element = element;
        }
        private string _element;

        public string Element
        {
            get { return _element; }
            set { _element = value; }
        }
    }
    /// <summary>
    /// 标识该字段是否属于节点的子节点，例如Button的caption存储在specific子节点中
    /// （目前还没有遇到其它情况）
    /// </summary>
    [AttributeUsage(AttributeTargets.Property)]
    public class SubNodeAttribute : Attribute
    {
        public SubNodeAttribute(string element)
        {
            this._element = element;
        }
        private string _element;
        /// <summary>
        /// 子节点名称（例如specific）
        /// </summary>
        public string Element
        {
            get { return _element; }
            set { _element = value; }
        }
    }
}
/*
 * 开发memo：
 * 没有做成继承，不同type的item，column好像也没有明显的继承关系
 */